package com.admin.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.TempOrderBean;

@Repository
public class TempOrderDao {

	@Autowired private JdbcTemplate jdbcTemplate;
	@Autowired private QueryRunner queryRunner;
	
	/**
	 * 插入新的临时订单表
	 * @param temp
	 * @return
	 */
	public int insertTempOrder(TempOrderBean temp){
		String sql = "insert into temp_order(telPhone,port,command,consume)values(?,?,?,?)";
		Object[] params = {temp.getTelPhone(),temp.getPort(),temp.getCommand(),temp.getConsume()};
		return jdbcTemplate.update(sql, params);
	}
	
	/**
	 * 查询
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public List<TempOrderBean> findTempOrder(int start,int pageSize){
		String sql = "select * from temp_order order by tempId desc limit ?,?";
		try {
			return queryRunner.query(sql, new BeanListHandler<TempOrderBean>(TempOrderBean.class), new Object[]{start,pageSize});
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	  * @Title: findCount
	  * @Description: 查询总数
	  * @param @return
	  * @return int
	  * @throws
	 */
	public int findCount(){
		String sql = "select count(*) from temp_order";
		return jdbcTemplate.queryForInt(sql);
	}

	/**
	  * @Title: delTempOrderById
	  * @Description:删除临时消费记录 
	  * @param tempId
	  * @return
	  * @throws
	 */
	public int delTempOrderById(int tempId) {
		return jdbcTemplate.update("delete from temp_order where tempId=?", tempId);
	}
	
	/**
	  * @Title: delTempOrderByTel
	  * @Description:根据手机号删除临时消费记录 
	  * @param @param telPhone
	  * @param @return
	  * @return int
	  * @throws
	 */
	public int delTempOrderByTel(String telPhone){
		String sql = "delete from temp_order where telPhone = ?";
		return jdbcTemplate.update(sql, telPhone);
	}
}
